/*
 * @Descripttion: 
 * @version: 
 * @Author: lsy
 * @Date: 2021-12-17 14:01:07
 * @LastEditors: lsy
 * @LastEditTime: 2021-12-18 13:49:15
 */

import "./qwebchannel.js"

let clientInstance
//创建客户端对象1
function initClientWidget() {
    return new Promise((resolve, reject) => {
        if (typeof window.cef3 === 'object') { //优先使用cef
            clientInstance = window.ClientObject
            resolve()
        }
        else if (typeof qt !== 'undefined') { //web端没有qt对象，客户端才有
            try {
                alert(213213213)
                console.log("initClientWidget++++++++++++++++++++++++++++")
                new QWebChannel(qt.webChannelTransport, function (channel) {
                    window.obj = channel.objects.ClientObj
                    clientInstance = channel.objects.ClientObj
                    resolve()
                })
            } catch (e) {
                console.log(e)
                reject(e)
            }
        } else {
            reject()
        }
    })
}
export let initClient = initClientWidget()
//通知客户端
export function nofifyClient(method, param) {
    try {
        console.log('=========前端=========function RPCCall====================')
        console.log('current method ==========' + method)
        // let paramStr = JSON.stringify(param)
        // console.log('param======'+paramStr)
        alert(clientInstance)
        clientInstance.MainCall(method, param);
    } catch (e) {
        console.log(e)
    }
}

export const clientObj = {
    //接收客户端消息
    connectRPCNotify(cb) {
        try {
            clientInstance.mainNotify.connect(cb)
        } catch (e) {
            console.log(e)
        }
    },
    //打开菜单
    openMenu(menucode) {
        nofifyClient('openMenu', menucode)
    },
    //隐藏页面
    hidePage() {
        nofifyClient('hidePage', '')
    },
    //打开导航页
    openCollect() {
        nofifyClient('openCollect', '')
    },
    //展开导航
    openColl() {
        nofifyClient('showHideRightPart', '{"showRightPart": true}')
    },
    //收起导航
    closeColl() {
        nofifyClient('showHideRightPart', '{"showRightPart": false}')
    }
}

//客户端调用更改 token
initClient.then(() => {
    console.info("客户端初始化成功")
    clientObj.connectRPCNotify((method, msg) => {
        console.log("客户端通知事件：" + msg)
        let data = msg
        if (method === 'changeToken') {
            localStorage.accessToken = data
        }
    })
}).catch(err => {
    // console.log(err)
})


// WEBPACK FOOTER //
// ./src/common/js/client.js